package cn.com.nes.site.service.system.company.impl;

import cn.com.nes.common.exception.BusinessException;
import cn.com.nes.mybatis.agent.ope.entity.TAgentIntentApplication;
import cn.com.nes.mybatis.agent.ope.mapper.TAgentIntentApplicationMapper;
import cn.com.nes.mybatis.agent.system.entity.Company;
import cn.com.nes.site.service.system.company.BusinessCompanyService;
import cn.com.nes.site.service.system.company.TAgentIntentApplicationService;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
@Slf4j
public class TAgentIntentApplicationServiceImpl extends ServiceImpl<TAgentIntentApplicationMapper, TAgentIntentApplication> implements TAgentIntentApplicationService {

    @Resource
    private BusinessCompanyService businessCompanyService;

    @Override
    public void supplementAgentIntentRecord(String manufacturerCompanyId, String agentCompanyId) throws Exception {
        if (StrUtil.isBlank(manufacturerCompanyId) || StrUtil.isBlank(agentCompanyId)) {
            log.error("参数异常:manufacturerCompanyId = {}, agentCompanyId = {}", manufacturerCompanyId, agentCompanyId);
            return;
        }

        // 查询厂商公司信息
        Company manufacturerCompanyInfo = businessCompanyService.getCompany(manufacturerCompanyId);
        if (ObjectUtil.isNull(manufacturerCompanyInfo)) {
            log.error("未查询到厂商信息");
            return;
        }

        // 查询代理商公司信息
        Company agentCompanyInfo = businessCompanyService.getCompany(agentCompanyId);
        if (ObjectUtil.isNull(agentCompanyInfo)) {
            log.error("未查询到代理商信息");
            return;
        }

        Date now = new Date();
        TAgentIntentApplication agentIntentApplication = new TAgentIntentApplication();
        // 设置必填项
        agentIntentApplication.setCompanyName(agentCompanyInfo.getCompanyname());
        agentIntentApplication.setLegalPerson(returnStringValue(agentCompanyInfo.getLegalperson()));
        agentIntentApplication.setLegalPersonIDCardNumber(StrUtil.EMPTY); // 获取不到法人身份证号码 - 不能为空,创建一个默认的
        agentIntentApplication.setUniformSocialCreditCode(returnStringValue(agentCompanyInfo.getUniformsocialcreditcode()));
        agentIntentApplication.setContactPerson(returnStringValue(agentCompanyInfo.getContact()));
        agentIntentApplication.setPosition("管理员(厂商主动拉取代理商)"); // 获取不到联系人职务 - 不能为空,造个默认的
        agentIntentApplication.setContactPhoneNumber(returnStringValue(agentCompanyInfo.getContactnumber()));
        agentIntentApplication.setContactEmail(StrUtil.EMPTY);   // 获取不到联系人邮箱 - 不能为空
        agentIntentApplication.setBusinessLicense(returnStringValue(agentCompanyInfo.getBusinesslicense()));
        agentIntentApplication.setTargetCompanyId(manufacturerCompanyId);
        agentIntentApplication.setState(1); // 由于是先建立代理关系,再补充申请记录,默认审核通过
        agentIntentApplication.setCreateUid(agentCompanyInfo.getGmtCreateId());
        agentIntentApplication.setCreateTime(now);
        // 设置非必填项
        agentIntentApplication.setAreaCode(agentCompanyInfo.getAreacode());
        agentIntentApplication.setIdCard(null);                  // 获取不到法人身份证照片
        agentIntentApplication.setIdCardBack(null);              // 获取不到法人身份证照片
        agentIntentApplication.setModifyUid(agentCompanyInfo.getGmtModifiedId());
        agentIntentApplication.setModifyTime(now);
        agentIntentApplication.setAgentId(agentCompanyId);
        agentIntentApplication.setRegistrationNumber(StrUtil.EMPTY);      // 获取不到营业执照注册号
        agentIntentApplication.setAddress(agentCompanyInfo.getAddress());

        int rows = this.baseMapper.insert(agentIntentApplication);
        if (rows != 1) {
            throw new BusinessException("补充代理申请记录失败");
        }
    }

    private String returnStringValue(String value) {
        return StrUtil.isBlank(value) ? StrUtil.EMPTY : value;
    }
}
